using System;
using System.Collections.Generic;
using ConsentManagementProviderLib.EventHandlerInterface;
using UnityEngine;
using UnityEngine.EventSystems;

namespace ConsentManagementProviderLib.Observer
{
	internal static class BroadcastEventDispatcher
	{
		public static Queue<Action> actions = new Queue<Action>();

		public static void Execute<T>(BaseEventData eventData, ExecuteEvents.EventFunction<T> functor) where T : IConsentEventHandler
		{
			IList<GameObject> handlersForEvent = BroadcastReceivers.GetHandlersForEvent<T>();
			if (handlersForEvent == null)
			{
				return;
			}
			CmpDebugUtil.Log($"{typeof(T).Name} has {handlersForEvent.Count} invokable instances");
			foreach (GameObject handler in handlersForEvent)
			{
				actions.Enqueue(delegate
				{
					ExecuteEvents.Execute(handler, eventData, functor);
				});
			}
		}
	}
}
